package com.fuyao.cloud.admin.flow.listener;

import cn.hutool.core.collection.CollUtil;
import com.fuyao.cloud.admin.flow.model.ExtendProcinst;
import com.fuyao.cloud.admin.flow.service.flowable.IDynamicCallbackService;
import com.fuyao.cloud.admin.flow.service.flowable.IExtendProcinstService;
import com.fuyao.cloud.common.core.flowable.enums.ProcessStatusEnum;
import com.fuyao.cloud.common.gray.support.NonWebVersionContextHolder;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.flowable.engine.impl.el.FixedValue;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * flowable 执行动态配置监听器
 */
@Slf4j
@Getter
@Setter
@Component(value = "executionBusinessCallListener")
public class ExecutionBusinessCallListener implements ExecutionListener {
    @Resource
    private IDynamicCallbackService dynamicCallbackService;
    @Resource
    private RuntimeService runtimeService;
    @Resource
    private TaskService taskService;
    @Resource
    private IExtendProcinstService extendProcinstService;
    /**
     * nacos的服务id
     */
    private FixedValue serviceId;
    /**
     * 访问的url路径
     */
    private FixedValue url;
    /**
     * 参数 多个的话用分号隔开 实例 userCode:00004737;status:1
     */
    private FixedValue params;

    @Value("${gray.rule.version}")
    private String version;

    @Override
    public void notify(DelegateExecution execution) {
        String processInstanceId = execution.getProcessInstanceId();
        String businessKey = execution.getProcessInstanceBusinessKey();
        String serviceIdStr = null, urlStr = null, paramsStr = null;
        if (serviceId != null) {
            serviceIdStr = serviceId.getExpressionText();
        }
        if (url != null) {
            urlStr = url.getExpressionText();
        }
        if (params != null) {
            paramsStr = params.getExpressionText();
        }
        Map<String, String> params = new HashMap<>();
        params.put("processInstanceId", processInstanceId);
        params.put("params", paramsStr);
        params.put("businessKey", businessKey);

        List<Task> taskList = taskService.createTaskQuery().processInstanceId(processInstanceId).list();

        ExtendProcinst extendProcinst = extendProcinstService.findExtendProcinstByProcessInstanceId(processInstanceId);
        // 处理终止和审批完结。
        if (CollUtil.isNotEmpty(taskList) && taskList.size() == 1) {
            if (ProcessStatusEnum.ZZ.toString().equals(extendProcinst.getProcessStatus())) {
                params.put(ProcessStatusEnum.ZZ.toString(), ProcessStatusEnum.ZZ.getMsg());
            } else {
                params.put(ProcessStatusEnum.BJ.toString(), ProcessStatusEnum.BJ.getMsg());
            }
            NonWebVersionContextHolder.setVersion(version);
            dynamicCallbackService.postCallback(serviceIdStr, urlStr, params);
        }
        // 多实例时不修改状态。
    }
}

